﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lotto
{
    class Lottery
    {

        private int maxValue = 0;
        private int nrOfDrawnNumbers;
        //private int nrOfWantedNumbers;
        private bool drawingIsOver;
        public int oneRndNum = 0;
        public int manyRndNums = 0;

        //variable to hold the last free index in the array of the user drawn numbers
        //next step is to use a list in both situations, then I won't need this variable.
        int nextIndex = 0;
        int storeNextIndex = 0;
        int _randomNumber;

        public int[] storeLottoNum;
        public int[] userLottoNum;

        Random rndm = new Random();

        //property
        //The maximum value of the property is set to 45 by the requierments
        public int MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value <= 45)
                    maxValue = value;
            }
        }

        public int NrOfDrawNumbers
        {
            //the number of already drawn numbers
            get { return nrOfDrawnNumbers; }
            set
            {
                nrOfDrawnNumbers = value;
            }
        }
        public int NrOfWantedNumbers
        {
            get;
            set;
        }

        public bool DrawingIsOver
        {
            get { return drawingIsOver; }
            set
            {
                drawingIsOver = value;
            }
        }

        //constructor
        public Lottery(int _maxValueOfLotto, int _numberOfWantedNumbers)
        {
            this.MaxValue = _maxValueOfLotto;
            this.NrOfWantedNumbers = _numberOfWantedNumbers;
            //create array to store the info
            storeLottoNum = new int[NrOfWantedNumbers];
            userLottoNum = new int[NrOfWantedNumbers];
            this.FillArrayWithRandomNumbers();
        }
        //methods

        /*
         * Method to fill in the array with numbers
         */
        public void FillArrayWithRandomNumbers()
        {
             bool unique = false;

            for (int i = 0; i < NrOfWantedNumbers; i++)  // for each number you want
            {
                do
                {
                    unique = true;                                     // Assume new value is unique
                    _randomNumber = rndm.Next(1, MaxValue) + 1;        // get the random number
                    for (int item = 0; item < storeNextIndex; item++)  // Check each element with a value in your array
                    {
                        if (storeLottoNum[item] == _randomNumber)      // if the value is the same
                        {
                            unique = false;       // It is not unique
                            break;                // break the loop, we don't care about the other values
                        }
                    }
                } while (unique == false);        // if it was not unique, try again

                storeLottoNum[storeNextIndex++] = _randomNumber;   // with unique value, save in the array and increment the 'storeNextIndex'
            }
        }


        //Draws a single number from up to 45. The user sets the max value.
        public void Draw1Number()
        {
            /*
             * The array is going to hold as many numbers 
             * as the user wants new int[NrOfWantedNumbers]
             */
            oneRndNum = rndm.Next(1, MaxValue);
            /*
             * In order to get only one number, the loop runs only once 
             * and stores the number in the userLottoNum Array
             * at the next free index position.
             */
            for (int i = 0; i < 1; i++)
            {
                userLottoNum[nextIndex] = oneRndNum;
                ++nextIndex;
            }
            NrOfDrawNumbers++;

            /*
             * Checking if the user has drawn enough numbers which he did set
             * in the first place. When the bool value is false the CheckIfYouWin()
             * should be called.
             */
            if (NrOfDrawNumbers == NrOfWantedNumbers)
                DrawingIsOver = true;
            else
            {
                DrawingIsOver = false;
                CheckIfYouWin();
                //missing functionality, once the drawing is over the whole form should be bloked for use
                //except a replay button
            }
        }
        //Draws as many numbers as the user has specified in "Number of wanted numbers" textBox
        public void DrawAllNumbers()
        {
            storeNextIndex = 0;
            bool unique = false;
            for (int i = 0; i < NrOfWantedNumbers; i++)
            {
                while (DrawingIsOver == false)
                {
                    do
                    {
                        unique = true;                                     // Assume new value is unique
                        _randomNumber = rndm.Next(1, MaxValue) + 1;        // get the random number
                        for (int item = 0; item < storeNextIndex - 1; item++)  // Check each element with a value in your array
                        {
                            if (userLottoNum[item] == _randomNumber)      // if the value is the same
                            {
                                unique = false;       // It is not unique
                                break;                // break the loop, we don't care about the other values
                            }
                        }
                    } while (unique == false);      // if it was not unique, try again
                    userLottoNum[storeNextIndex++] = _randomNumber;
                    NrOfDrawNumbers++;


                    if (NrOfDrawNumbers == NrOfWantedNumbers)
                        DrawingIsOver = true;
                    else
                    {
                        DrawingIsOver = false;
                    }
                }
            }
        }

        public bool CheckIfYouWin()
        {
            int trueth = 0;
            int b = userLottoNum.Count();
            bool hasDuplicate = false;

            foreach (var itemFirsArray in userLottoNum)
            {
                foreach (var itemSecondArray in storeLottoNum)
                {
                    if (itemFirsArray == itemSecondArray)
                    {
                        trueth++;
                    }
                }
            }
            if (trueth == b)
            {
                hasDuplicate = true;
            }
            return hasDuplicate;
        }
    }
}
